package com.rongmai.web.inteceptors;

import com.alibaba.fastjson.JSON;
import com.rongmai.db.model.BaseResult;
import com.rongmai.db.model.User;
import com.rongmai.db.utils.DateUtils;
import com.rongmai.web.bo.UserBo;
import com.rongmai.web.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;

@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

	@Resource
	private UserBo userBo;

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
	 */
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {

	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
	 */
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {

	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object arg2) throws Exception {

		String path = request.getServletPath();
		if (path.contains("common") || path.contains("file") ||path.contains("news") || path.equals("/")) {
			return true;
		}

		User currentUser = UserUtils.getCurrentUser(request);
		if (currentUser == null) {
			String sessionId = request.getHeader("sessionId");
			HttpSession session = request.getSession();
			currentUser = userBo.getBySessionId(sessionId);
			session.setAttribute(UserUtils.currentUser, currentUser);
		}

		if (currentUser == null) {
			log.info("兜底当前用户");
			currentUser = userBo.getByMobileAndRole("11111111115", "FINANCE");
			HttpSession session = request.getSession();
			session.setAttribute(UserUtils.currentUser, currentUser);
			return true;
		}

		if (currentUser == null) {
			if (path.contains("getMobile") || path.contains("login")) {
				return true;
			}

			goToLogin(request, response);
			return false;
		}
		log.info("当前登录用户id:{},用户名:{}", currentUser.getId(), currentUser.getName());
//		long createTime = currentUser.getCreateTime().getTime();
//		long currentTime = System.currentTimeMillis();
//		if (currentTime - createTime > 1000L*60*60*24*30) {
//			goToLogin(request, response);
//			return false;
//		}
		return true;
	}

	public static void main(String[] args) {
		long time = DateUtils.parseDate("2024-01-23 07:42:32").getTime();
		long currentTime = System.currentTimeMillis();
		long l = currentTime - time;
		System.out.println(l>1000L*60*60*24*30);

	}

	private void goToLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
		response.setCharacterEncoding(StandardCharsets.UTF_8.name());
		response.getWriter().write(JSON.toJSONString(BaseResult.TO_LOGIN));
		request.getSession().removeAttribute(UserUtils.currentUser);
	}

}
